﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;


namespace MO.ModelingClient
{
    public partial class EntityForm : Form
    {
        public EntityForm()
        {
            InitializeComponent();
            tableControl1.AfterInsert = tableControl_AfterInsert;
            tableControl1.AfterDelete = tableControl_AfterDelete;
        }

        void tableControl_AfterInsert(Dictionary<string, object> arrs)
        {
            string tableName = arrs["ED_Code"].ToString();
            Dictionary<string, object> dict = new Dictionary<string, object>();
            dict["FD_CODE"] = tableName + "_CODE";
            dict["FD_Name"] = "编码";
            dict["ED_Code"] = tableName;
            dict["FD_Type"] = FieldType.String;
            dict["FD_Length"] = 50;
            dict["FD_NotNull"] = true;
            dict["FD_Order"] = 0;
            DbOperation.DB.Insert("Field_Define", dict);
            dict = new Dictionary<string, object>();
            dict["FD_CODE"] = tableName + "_NAME";
            dict["FD_Name"] = "名称";
            dict["ED_Code"] = tableName;
            dict["FD_Type"] = FieldType.String;
            dict["FD_Length"] = 100;
            dict["FD_NotNull"] = true;
            dict["FD_Order"] = 1;
            DbOperation.DB.Insert("Field_Define", dict);
            if ((bool)arrs["ED_Parent"])
            {
                dict = new Dictionary<string, object>();
                dict["FD_CODE"] = tableName + "_PARENT";
                dict["FD_Name"] = "上级" + arrs["ED_Name"];
                dict["ED_Code"] = tableName;
                dict["FD_Type"] = FieldType.String;
                dict["FD_Length"] = 50;
                dict["FD_NotNull"] = false;
                dict["FD_Order"] = 2;
                DbOperation.DB.Insert("Field_Define", dict);
            }
        }
        void tableControl_AfterDelete(string key)
        {
            DbOperation.DB.ExecuteQuery("delete from Field_Define where ED_Code = {0} ", key);
        }
        private void EntityForm_Load(object sender, EventArgs e)
        {

        }

        private void EntityForm_Shown(object sender, EventArgs e)
        {
            DataConfig config = new DataConfig();
            config.TableName = "Entity_Define";
            config.KeyField = "ED_Code";
            config.TitleField = "ED_Name";
            config.AddItem(new ItemData("ED_Code", "编码"));
            config.AddItem(new ItemData("ED_Name", "名称"), true);
            config.AddItem(new ItemData("ED_Description", "描述"));
            config.AddItem(new ItemData("ED_Parent", "树型结构") { Type = DataType.Bool });
            config.AddItem(new ItemData("ED_IsEnum", "是否为枚举字典") { Type = DataType.Bool });
            tableControl1.Init(config);
        }
    }
}
